package cn.handong.tool.沪房;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ReadExcelTest {
    //中建熙江岳
    @Test
    public void readExcelAndWriteTxt() {
        String fileName = "xjy.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 同步读取会自动finish
        List<HouseData> list = EasyExcel.read(fileName)
                .head(HouseData.class)
                .sheet()
                .headRowNumber(1)
                .doReadSync();
        List<HouseData> lists = list.stream().filter(
                item -> item.getTotalPrice() <= 4500000 //总价低于450w
//                        && (item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '2'
//                        || item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '3')
//                        && item.getMeasure() >= 90     //大于等于90平
//                        &&
//                        item.getFlourNumber() >= 5  //大于等于3楼
//                && item.getBuildingNumber() != 6
//
//                && item.getBuildingNumber() != 10
                &&((item.getBuildingNumber() == 6 && item.getFlourNumber() >= 3)  //6号楼 >= 3楼
                    || (item.getBuildingNumber() == 10 && item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '1' && item.getFlourNumber() >= 5)   //10号楼 >= 5楼
                    || (item.getBuildingNumber() == 10  && item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '2' && item.getFlourNumber() >= 8)
                    || (item.getBuildingNumber() == 10  && item.getRoomNumber().charAt(item.getRoomNumber().length() - 1) == '3' && item.getFlourNumber() >= 8)
                )
//
//                && item.getFlourNumber() != 1
//
//                && item.getFlourNumber() != 2

                && item.getMeasure() >=90  //3 + 2 + 2

                && item.getFlourNumber() < 18  //不要顶楼

//                        &&
//                        item.getBuildingNumber() == 12
//                (item.getBuildingNumber() == 6 && item.getFlourNumber() >= 3) ||
//                (item.getBuildingNumber() != 6 && item.getFlourNumber() >= 5))
//                &&((item.getBuildingNumber() == 10) && !(Objects.equals(item.getRoomNumber(), "1001")))
        ).sorted().collect(Collectors.toList());

        //写入txt
        String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
        String file = "test.txt";
        List<String> messageList = new ArrayList<>();
        for (HouseData houseData : lists) {
            messageList.add(houseData.getBuildingNumber() + "-" + houseData.getRoomNumber() + "-" + houseData.getTotalPrice());
        }
        try {
            writeToTxt(folder, file, messageList);
        } catch (FileNotFoundException e) {
//            logger.error("Error Message :", e);
        }

    }

    /**
     * 逐行读取TXT
     */
    @Test
    public void readTxtToObject() {
        try {
            String folder = "D:\\Idea_WorkSpace\\my-general-tool\\src\\main\\resources";
            String path = "test.txt";
            BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(path)), StandardCharsets.UTF_8));
            String lineTxt;
            // 逐行读取
            while ((lineTxt = br.readLine()) != null) {
                String[] split = lineTxt.split("-");
                Integer price = Integer.parseInt(split[2]);  //总价
                Integer bn = Integer.parseInt(split[0]);     //楼栋
                Integer floorNum = Integer.parseInt(split[1].substring(0,split[1].length() - 2)); //楼层
                System.out.println(lineTxt);
            }
            br.close();
        } catch (Exception e) {
//            logger.error("Error Message :"+ e);
        }
    }

    public void writeToTxt(String folder, String file, List<String> messageList) throws FileNotFoundException {
        File files = new File(folder);
        files.mkdirs();

        FileOutputStream fos = new FileOutputStream(folder + File.separator + file);

        // 逐行写入
        PrintWriter pw = new PrintWriter(fos);
        for (String message : messageList) {
            pw.println(message);
        }
        pw.close();

    }

    @Test
    public void makeExcel(){
        //D43+D44*E44
        StringBuilder sb = new StringBuilder();
        for (int i = 43; i < 65; i++) {
            sb.append("D"+i+"*E"+i+"+");
        }
        System.out.println(sb);
    }
}